package faceQuestion;

/**
 * Description： TODO
 * Author： 王多鱼
 * CreateTime： 2021/6/29 19:04
 * String padString(String string,int minLength,char padChar)，
 * 就是在string前用padChar将string填充至minLength的长度，
 * 如("7",3,'0')得到的结果是"007"，("2012",3,'0')得到的结果是"2012"。minLength不能为负数。
 **/
public class PadString {
    public static void main(String[] args) {
        System.out.println(padString("7", 3, '0'));
        System.out.println(padString("2012", 3, '0'));
    }

    private static String padString(String string, int minLength, char padChar) {
        int n = string.length();
        StringBuffer currentString = new StringBuffer();
        if (minLength <= n || minLength < 0)
            return string;
        else {
            int m = minLength - n;
            for (int i = 0; i < m; i++) {
                currentString.append(padChar);
            }
            currentString.append(string);
        }
        return currentString.toString();
    }
}
